


import tkinter as tk
import tkinter.filedialog
from tkinter import *

from tkcalendar import Calendar
import datetime
from tkinter import ttk



import threading  # 导入多线程模块
from main import init_clip


file_name=''
table_no=''
enevt_id=''
timestamp_ms=0
def create_windows():
    '''初始化窗口'''
    root  = tk.Tk()
    root .title("风氏")
    root .geometry("800x600")
    root .resizable(width=False, height=False)
    return root 



def deploy_play(root ):
    
    lab=tk.Label(root,text='赛事ID：')
    ent=tk.Entry(root)
    lab.grid(row=0, column=0,padx=(10, 0),pady=(20,10)) 
    ent.grid(row=0, column=1,padx=(0, 0),pady=(20,10)) 

    lab1=tk.Label(root,text='场地ID：')
    ent1=tk.Entry(root)
    lab1.grid(row=0, column=2,padx=(10, 0),pady=(20,10)) 
    ent1.grid(row=0, column=3,padx=(0, 0),pady=(20,10)) 

    def xz():
        global file_name
        file_name=tkinter.filedialog.askopenfilename()
        if file_name != '':
            btn2.config(text=file_name)
        else:
            btn2.config(text='您没有选择任何文件')
            
    lab2=tk.Label(root,text='原始视频地址：')
    btn2=tk.Button(root,text='文件选择',width=10,padx=10, pady=0,command=xz,anchor="e")
    lab2.grid(row=1, column=0,padx=(10, 0),pady=(20,10))
    btn2.grid(row=1, column=1,padx=(0, 0),pady=(20,10))

               
    def start_calendar():
        def print_sel():
            start_time_text.configure(state="normal")
            print(str(cal.selection_get()) + " " + str(hour.get()) + ":" + str(minute.get())+ ":" + str(second.get()))
            s_data = str(cal.selection_get()) + " " + str(hour.get()) + ":" + str(minute.get())+ ":" + str(second.get())
            start_time_text.delete(0, END)
            start_time_text.insert("0", s_data)
            # start_time_text.configure(state="disabled")
            # cal.see(datetime.date(year=2016, month=2, day=5))
            top.destroy()
        
        top = tk.Toplevel()
        top.geometry("300x250")
    
    
        cal = Calendar(top, font="Arial 14", selectmode='day', locale='zh_CN')
        cal.place(x=0, y=0, width=300, height=200)
        values_h = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                    20, 21, 22, 23]
        values_m = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                    20, 21, 22, 23, 24, 25, 26, 27, 28,
                    29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
                    54,
                    55, 56, 57, 58, 59]
    
        values_s = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                    20, 21, 22, 23, 24, 25, 26, 27, 28,
                    29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
                    54,
                    55, 56, 57, 58, 59]
        hour = ttk.Combobox(
            master=top,  # 父容器
            height=15,  # 高度,下拉显示的条目数量
            width=3,  # 宽度
            state="normal",  # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
            cursor="arrow",  # 鼠标移动时样式 arrow, circle, cross, plus...
            font=("", 14),  # 字体
            values=values_h,  # 设置下拉框的选项
        )
        hour.place(x=0, y=200)
        ttk.Label(top, text="时").place(x=60, y=195, width=20, height=40)
    
        minute = ttk.Combobox(
            master=top,  # 父容器
            height=15,  # 高度,下拉显示的条目数量
            width=3,  # 宽度
            state="normal",  # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
            cursor="arrow",  # 鼠标移动时样式 arrow, circle, cross, plus...
            font=("", 14),  # 字体
            values=values_m,  # 设置下拉框的选项
        )
        minute.place(x=80, y=200)
        ttk.Label(top, text="分").place(x=140, y=195, width=20, height=40)

        second = ttk.Combobox(
            master=top,  # 父容器
            height=15,  # 高度,下拉显示的条目数量
            width=3,  # 宽度
            state="normal",  # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
            cursor="arrow",  # 鼠标移动时样式 arrow, circle, cross, plus...
            font=("", 14),  # 字体
            values=values_s,  # 设置下拉框的选项
        )
        second.place(x=160, y=200)
        ttk.Label(top, text="秒").place(x=220, y=195, width=20, height=40)
        tk.Button(top, text="确定", command=print_sel).place(x=240, y=205)

    start_time = tk.Button(root, text="开始时间", command=start_calendar)
    start_time.grid(row=1, column=2,padx=(10, 0),pady=(20,10))
    start_time_text = tk.Entry(root, width=20)
    start_time_text.grid(row=1, column=3,padx=(10, 0),pady=(20,10))
   
   
    def submit():
        global file_name
        global enevt_id
        global table_no
        global timestamp_ms
        enevt_id = ent.get()
        table_no = ent1.get()

        # 获取自纪元以来的毫秒数
        date_string=start_time_text.get()
        date_object = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
        timestamp_ms = int(date_object.timestamp() * 1000)
        start_thread()
    btn=tk.Button(root,text='裁剪',command=submit)
    btn.grid(row=1, column=4,padx=30,pady=(20,10))


def start_thread():
    T = threading.Thread(target=init_clip,args=(file_name,enevt_id,table_no,timestamp_ms))  # 多线程
    T.setDaemon(True)  # 线程守护，即主进程结束后，此线程也结束。否则主进程结束子进程不结束
    T.start()  # 启动
    T.join()
    print('完成')

def main():
    '''主函数'''
    # 初始化窗口
    root = create_windows()
    # 运行窗口
    deploy_play(root )

    # 部署ScrolledText控件
    # 进入主循环
    root.mainloop()


if __name__ == '__main__':
    main()
